
$dir = `ls ./nonref/`;
@dir =split(/\t/,$dir);
#print "@dir\n";
open(WRBUFF,">result_final_linc_chr21") or die "no able to write\n";
for($z=1;$z<@dir;$z++)
{
       open(BUFF,"./nonref/$dir[$z]") or die "no file found ./nonref/$dir[$z]\n";
       open(BUFF1,"./ref/$dir[$z]") or die "no file found ./ref/$dir[$z]\n";
#open(BUFF,"chr21") or die "no file found ./nonref/nonref\n";
#open(BUFF1,"chr21_ref") or die "no file found\n";
$x = 0;
        $line1=<BUFF1>;
        chomp $line1;
        @array1 =split(/\t/,$line1);
        $chrname_cds = $array1[0];
 $tstarts = $array1[1];
        $tstops = $array1[2];
        while($line=<BUFF>)
        {
                $x++;
          #      print "Line number $x\n";
                chomp $line;
                $k =0;
                @array =split(/\t/,$line);
                $readposition =$array[3];
                $chrname = $array[2];
                #print $chrname."\t".$readposition."\t".$chrname_cds."\t".$tstarts."\t".$tstops."\n";
                #while($chrname ne $chrname_cds||($readposition > $tstops && $chrname eq $chrname_cds))
                while($readposition > $tstops)
		{
                                #       die;
                        $line1=<BUFF1> or last;
                        chomp $line1;
                        @array1 =split(/\t/,$line1);
                        $chrname_cds = $array1[0];
                        $tstarts = $array1[1];
                        $tstops = $array1[2];
                        #print "second\t".$chrname."\t".$readposition."\t".$chrname_cds."\t".$tstarts."\t".$tstops."\n";
                        #last;
                }

                if($readposition <= $tstops && $readposition >= $tstarts && $chrname eq $chrname_cds)
                {
                        $k =1;
                        #print $line."\n";
                }
                if($k == 0)
                {
                        $dist = $readposition-$tstarts;
                        print WRBUFF $line."\t".$dist."\t".$array1[4]."\n";
                }
        }
}
